/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import dao.UsuarioDAO;
import entity.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.MD5;

/**
 *
 * @author Tiago
 */
@WebServlet(name = "ProcessaLogin", urlPatterns = {"/ProcessaLogin"})
public class ProcessaLogin extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {

        response.setContentType("text/html;charset=UTF-8");

        String action = request.getParameter("action");

        if ("login".equals(action)) {

            String login = request.getParameter("campoLogin");
            String senha = request.getParameter("campoSenha");

            MD5 md5 = new MD5();
            String senhacrip = md5.ConvertMD5(senha);            
            
            UsuarioDAO daouser = new UsuarioDAO();
            Usuario user = new Usuario();

            user = daouser.login(login, senhacrip);

            
            GregorianCalendar cal = new GregorianCalendar();
            user.setDiaLogin(cal.getTime());
            
            int add = 5;
            int i = 0;
            while (i < add) {
                cal.add(Calendar.DAY_OF_MONTH, 1);
                int ds = cal.get(Calendar.DAY_OF_WEEK);
                if (ds != Calendar.SATURDAY && ds != Calendar.SUNDAY) {
                    i++;
                }
            }            

            user.setDiaCalculadoLogin(cal.getTime());

            if ("tecnico".equals(user.getCargo())) {
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                //response.sendRedirect("tecnico.jsp");
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/tecnico.jsp");
                
                rd.forward(request, response);
            } else if ("gerente".equals(user.getCargo())) {
                HttpSession session = request.getSession();
                session.setAttribute("user", user);

                RequestDispatcher rd = getServletContext().getRequestDispatcher("/gerente.jsp");
                rd.forward(request, response);
            } else {
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
                rd.forward(request, response);                
            }

        } else if (action.equals("logout")) {
            HttpSession session = request.getSession();
            session.invalidate();
            RequestDispatcher rd = request.getServletContext().getRequestDispatcher("/index.jsp");
            rd.forward(request, response);

        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);

        } catch (SQLException ex) {
            Logger.getLogger(ProcessaLogin.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);

        } catch (SQLException ex) {
            Logger.getLogger(ProcessaLogin.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
